/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.shiro;

import com.bjboot.caocao.system.domain.UserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.security.Principal;
import java.util.Collection;
import java.util.List;

/**
 *@Description Shiro工具
 *@Author 操文健
 *@Date 2018-09-06 上午10:42
 *@Version 1.0
 **/
public class ShiroUtils {
    @Autowired
    private static SessionDAO sessionDAO;

    // 获取当前用户的的subject主体
    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }

    // 从Apache的shiro中获取用户信息
    public static UserDO getUser() {
        Object object = getSubjct().getPrincipal();
        return (UserDO)object;
    }
    public static Long getUserId() {
        return getUser().getUserId();
    }

    /**
     * 当前用户退出登陆
     */
    public static void logout() {
        getSubjct().logout();
    }

    // 获取系统中活动的sessions信心
    public static List<Principal> getPrinciples() {
        List<Principal> principals = null;
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        return principals;
    }
}
